#!/bin/sh
#
# Copyright (C) 2002 Yixiong Zou (yixiong.zou@intel.com)
# Support:      linux-ha-dev@lists.tummy.com
# License:      GNU General Public License (GPL)
#
set +x

#
#	BasicSanityCheck has to run as root.
#	
#	Perhaps a better way to do this is to use the cl_status command...
#		-- AlanR
#	
NODE0=`uname -n`
NODE1=imalwaysdead.com

# make sure the agent and the subagent are dead
killall -9 hbagent
killall -9 snmpd

# make sure the snmpd is running and agentX support is turned on
GenerateSNMPDConf() {
	cat <<-! >$1
		rocommunity public
		master yes
		trap2sink localhost
		!
	chmod 644 $1
}

which snmpd
ret=$?
if test $ret != 0 
	then echo "snmpd file is not in the path, skip agent test..."
	exit $ret
fi

# start the snmpd manually with our own config file
SNMPDCONF=/var/run/testsnmpd.conf
SNMPPIDFILE=/var/run/testsnmpd.pid
SNMPD=`which snmpd`
GenerateSNMPDConf $SNMPDCONF
$SNMPD -C -c $SNMPDCONF -p $SNMPPIDFILE

#@INITDIR@/snmpd status 
#ret=$?
#if test $ret != 0 
#	then echo "snmpd cannot be started correctly. skip agent test..."
#	exit $ret
#else 
#	echo "snmpd is already running, good"
#fi

# we want to wait for a couple seconds to let the master agent fully
# ready before we start the subagent.
sleep 3


# start the linux-ha snmp subagent
@libdir@/heartbeat/hbagent -d &
HBAGENTPID=$!
sleep 5

# get the nodename for node0 and node1
# node0 should be the value of the localhost
# node1 should be "ImAlwaysDead.com"

export MIBS=ALL
#snmpwalk -v2c localhost -c public LinuxHA
node0=`snmpget -v2c localhost -c public LHANodeName.1 | sed -ne 's/LINUX-HA-MIB::LHANodeName.1 = STRING: //p'`
echo node0 = $node0
node1=`snmpget -v2c localhost -c public LHANodeName.2 | sed -ne 's/LINUX-HA-MIB::LHANodeName.2 = STRING: //p'`
echo node1 = $node1

ret=1
ret2=1
for name in $NODE0 $NODE1; do
	echo name = $name, node0 = $node0
	if test "$node0" = "$name"; then
		ret=0
		echo "found $node0"
		break
	fi
done

if test $ret = 0; then
	for name in $NODE0 $NODE1; do
		echo name = $name, node1 = $node1
		if test "$node1" = "$name"; then
			ret2=0
			echo "found $node1"
			break
		fi
	done
fi

if test $ret2 = 0; then
	echo "BasicSanityCheck for SNMP Subagent passed."
#	exit 0
else 
	echo "BasicSanityCheck for SNMP Subagent failed."
#	exit 1
fi

#
# BasicSanityCheck for SNMP Subagent about CRM resources.
#

# wait to let cib ready to execute variant_op
sleep 10

# add CRM resource.
RSC0NAME="prmDummy"
RSC0TYPE="primitive(1)"
RSC0NODE="$NODE0"
RSC0STATUS="started(2)"
RSC0ISMANAGED="managed(1)"
RSC0FAILCOUNT="0"
RSC0PARENT=""

# clear resources
cibadmin --cib_replace -X '<resources/>'
cibadmin --cib_create -o resources \
  -X '<primitive id="prmDummy" class="ocf" type="Dummy" provider="heartbeat"/>'
sleep 3

# get resource's information with snmp subagent.
rsc0name=`snmpget -v2c localhost -c public LHAResourceName.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceName.1 = STRING: //p'`
rsc0type=`snmpget -v2c localhost -c public LHAResourceType.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceType.1 = INTEGER: //p'`
rsc0node=`snmpget -v2c localhost -c public LHAResourceNode.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceNode.1 = STRING: //p'`
rsc0status=`snmpget -v2c localhost -c public LHAResourceStatus.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceStatus.1 = INTEGER: //p'`
rsc0ismanaged=`snmpget -v2c localhost -c public LHAResourceIsManaged.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceIsManaged.1 = INTEGER: //p'`
rsc0failcount=`snmpget -v2c localhost -c public LHAResourceFailCount.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceFailCount.1 = INTEGER: //p'`
rsc0parent=`snmpget -v2c localhost -c public LHAResourceParent.1 \
  | sed -ne 's/LINUX-HA-MIB::LHAResourceParent.1 = STRING: //p'`

# check for LHAResourceName
ret=0
echo "rsc0name = $rsc0name, RSC0NAME = $RSC0NAME" 
if test "$rsc0name" != "$RSC0NAME"; then
	echo "failed to get resource name." >&2
	ret=1
fi

# check for LHAResourceType
if test $ret = 0; then
	echo "rsc0type = $rsc0type, RSC0TYPE = $RSC0TYPE" 
	if test "$rsc0type" != "$RSC0TYPE"; then
		echo "failed to get resource type." >&2
		ret=1
	fi
fi

# check for LHAResourceNode
if test $ret = 0; then
	echo "rsc0node = $rsc0node, RSC0NODE = $RSC0NODE" 
	if test "$rsc0node" != "$RSC0NODE"; then
		echo "failed to get resource node." >&2
		ret=1
	fi
fi

# check for LHAResourceStatus
if test $ret = 0; then
	echo "rsc0status = $rsc0status, RSC0STATUS = $RSC0STATUS" 
	if test "$rsc0status" != "$RSC0STATUS"; then
		echo "failed to get resource status." >&2
		ret=1
	fi
fi

# check for LHAResourceIsManaged
if test $ret = 0; then
	echo "rsc0ismanaged = $rsc0ismanaged, RSC0ISMANAGED = $RSC0ISMANAGED" 
	if test "$rsc0ismanaged" != "$RSC0ISMANAGED"; then
		echo "failed to get resource ismanaged." >&2
		ret=1
	fi
fi
# check for LHAResourceFailCount
if test $ret = 0; then
    echo "rsc0failcount = $rsc0failcount, RSC0FAILCOUNT = $RSC0FAILCOUNT"
    if test "$rsc0failcount" != "$RSC0FAILCOUNT"; then
        echo "failed to get resource failcount." >&2
        ret=1
    fi
fi

# check for LHAResourceParent
if test $ret = 0; then
    echo "rsc0parent = $rsc0parent, RSC0PARENT = $RSC0PARENT"
    if test "$rsc0parent" != "$RSC0PARENT"; then
        echo "failed to get resource parent." >&2
        ret=1
    fi
fi

# show the result.
if test $ret = 0; then
	echo "BasicSanityCheck for SNMP Subagent about CRM resources passed."
else 
	echo "BasicSanityCheck for SNMP Subagent about CRM resources failed."
fi

kill $HBAGENTPID
if
  [ -f $SNMPPIDFILE -a ! -z $SNMPPIDFILE ]
then
  kill `cat $SNMPPIDFILE`
  rm -f $SNMPPIDFILE
fi
# clear resources
cibadmin --cib_replace -X '<resources/>'

exit $ret
